package com.opta.controller;

import com.opta.entity.Line;
import com.opta.entity.ModelInfo;
import com.opta.entity.OrdersAndLines;
import com.opta.entity.TaskOrder;
import com.opta.service.OptaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 2 * @Author: GuanChong
 * 3 * @Date: 2020/9/30 14:10
 * 4
 */
@RestController
@RequestMapping("/opta")
public class OptaController {
    @Autowired
    private OptaService OptaService;

    /**
     * 调用引擎
     */
    @RequestMapping("/solve")
    public String solve(String type) {
        /*
           校验数据等操作 确保数据的合理性
         */
        //构造数据 测试用
        OrdersAndLines datas = new OrdersAndLines();
        //测试效率 initDataTestEfficiency(datas);
        initDataTestEfficiency(datas);

        OptaService.orderAndSupplyService(datas, type);
        return "success!";
    }

    /**
     * 构造测试数据 不拆单
     *
     * @param datas
     */
    private void initData(OrdersAndLines datas) {
        //赋值待排任务
        List<TaskOrder> orders = new ArrayList<>(16);
        TaskOrder taskOrder1 = new TaskOrder();
        ModelInfo modelInfo1 = new ModelInfo();
        modelInfo1.setMaterialCode("a");
        taskOrder1.setOrderId("1");
        taskOrder1.setModelInfos(modelInfo1);
        taskOrder1.setDeliveryDate(LocalDate.parse("2020-11-13"));
        taskOrder1.setOrderQty(100);

        TaskOrder taskOrder2 = new TaskOrder();
        ModelInfo modelInfo2 = new ModelInfo();
        modelInfo2.setMaterialCode("b");
        taskOrder2.setOrderId("2");
        taskOrder2.setModelInfos(modelInfo2);
        taskOrder2.setDeliveryDate(LocalDate.parse("2020-12-13"));
        taskOrder2.setOrderQty(100);


        TaskOrder taskOrder3 = new TaskOrder();
        ModelInfo modelInfo3 = new ModelInfo();
        modelInfo3.setMaterialCode("c");
        taskOrder3.setOrderId("3");
        taskOrder3.setModelInfos(modelInfo3);
        taskOrder3.setDeliveryDate(LocalDate.parse("2020-12-13"));
        taskOrder3.setOrderQty(110);


        TaskOrder taskOrder4 = new TaskOrder();
        ModelInfo modelInfo4 = new ModelInfo();
        modelInfo4.setMaterialCode("d");
        taskOrder4.setOrderId("4");
        taskOrder4.setModelInfos(modelInfo4);
        taskOrder4.setDeliveryDate(LocalDate.parse("2020-12-13"));
        taskOrder4.setOrderQty(111);

        orders.add(taskOrder1);
//        orders.add(taskOrder2);
//        orders.add(taskOrder3);
//        orders.add(taskOrder4);

        //赋值供应线体
        List<Line> lines = new ArrayList<>(16);
        Line line1 = new Line();
        line1.setLineId("l1");
        line1.setTeam("t1");
        line1.setCapacity(90);
        line1.setDate(LocalDate.parse("2020-10-11"));

        Line line2 = new Line();
        line2.setLineId("l2");
        line2.setTeam("t2");
        line2.setCapacity(200);
        line2.setDate(LocalDate.parse("2020-10-12"));

        Line line3 = new Line();
        line3.setLineId("l3");
        line3.setTeam("t3");
        line3.setCapacity(200);
        line3.setDate(LocalDate.parse("2021-10-13"));

        Line line4 = new Line();
        line4.setLineId("l4");
        line4.setTeam("t4");
        line4.setCapacity(200);
        line4.setDate(LocalDate.parse("2021-10-13"));

        Line line5 = new Line();
        line5.setLineId("l5");
        line5.setTeam("t5");
        line5.setCapacity(200);
        line5.setDate(LocalDate.parse("2021-10-13"));


        lines.add(line1);
        lines.add(line2);
        lines.add(line3);
        lines.add(line4);
        lines.add(line5);


        Map<String, Map<String, Double>> materialProducibleTeamMap = new HashMap<>(16);
        Map<String, Double> takt1 = new HashMap<>(16);
        takt1.put("t1", 1.0);
        takt1.put("t5", 1.0);
        materialProducibleTeamMap.put("a", takt1);

        Map<String, Double> takt2 = new HashMap<>(16);
        takt2.put("t2", 1.0);
        materialProducibleTeamMap.put("b", takt2);


        Map<String, Double> takt3 = new HashMap<>(16);
        takt3.put("t3", 1.0);
        materialProducibleTeamMap.put("c", takt3);


        Map<String, Double> takt4 = new HashMap<>(16);
        takt4.put("t4", 1.0);
        materialProducibleTeamMap.put("d", takt4);


        datas.setOrders(orders);
        datas.setLines(lines);
        datas.setMaterialProducibleTeamMap(materialProducibleTeamMap);
        datas.setVersionName("test");
        datas.setCompany("1000");


    }


    /**
     * @param datas
     */
    private void initDataTestEfficiency(OrdersAndLines datas) {
        //赋值待排任务
        List<TaskOrder> orders = new ArrayList<>(16);

        for (int i = 0; i < 500; i++) {
            TaskOrder taskOrder = new TaskOrder();
            ModelInfo modelInfo = new ModelInfo();
            modelInfo.setMaterialCode("a" + i);
            taskOrder.setOrderId(i + "");
            taskOrder.setModelInfos(modelInfo);
            taskOrder.setDeliveryDate(LocalDate.parse("2020-11-13"));
            taskOrder.setOrderQty(100);
            orders.add(taskOrder);
        }

        //赋值供应线体
        List<Line> lines = new ArrayList<>(16);
        for (int i = 0; i < 50; i++) {
            Line line = new Line();
            line.setLineId("" + i);
            line.setTeam("t" + i);
            line.setCapacity(100);
            line.setDate(LocalDate.parse("2020-10-11"));
            lines.add(line);
        }


        Map<String, Map<String, Double>> materialProducibleTeamMap = new HashMap<>(16);

        for (int i = 0; i < 500; i++) {
            Map<String, Double> takt = new HashMap<>(16);
            takt.put("t" + i, 1.0);
            materialProducibleTeamMap.put("a" + i, takt);
        }

        datas.setOrders(orders);
        datas.setLines(lines);
        datas.setMaterialProducibleTeamMap(materialProducibleTeamMap);
        datas.setVersionName("test");
        datas.setCompany("1000");


    }


}
